<template>
    <div class="divShow">
        <el-row>
            <el-col :span="24">
                <div style="padding: 30px;">
                    <!-- 搜索框 -->
                    <el-form :inline="true" :model="searchUnit" class="demo-form-inline" v-if="showSearch">
                        <el-form-item label="单位编码">
                            <el-input v-model="searchUnit.measureCode" placeholder="请输入单位编码"></el-input>
                        </el-form-item>
                        <el-form-item label="单位名称">
                            <el-input v-model="searchUnit.measureName" placeholder="请输入单位名称"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="checkListUnit">查询</el-button>
                            <el-button @click="cleanMdItem">重置</el-button>
                        </el-form-item>
                    </el-form>

                    <!-- 按钮 -->
                    <el-row>
                        <el-col :span="22">
                            <el-button type="primary" size="small" @click="changeDialog(null)">新增</el-button>
                            <el-button type="success" size="small" :disabled="!judgmentCh"
                                @click="changeDialog(SelMeasureId)">修改</el-button>
                            <el-button type="danger" size="small" :disabled="!judgmentDe"
                                @click="delJudgDialog(arrayMeasureId)">删除</el-button>
                        </el-col>
                        <el-col :span="2">
                            <div>
                                <el-tooltip content="隐藏搜索" placement="top">
                                    <el-button icon="el-icon-search" circle @click="toggleSearch"></el-button>
                                </el-tooltip>
                                <el-tooltip content="刷新" placement="top">
                                    <el-button icon="el-icon-refresh" circle @click="refresh"></el-button>
                                </el-tooltip>
                            </div>
                        </el-col>
                    </el-row>



                    <!-- 表格 -->
                    <el-table :data="UnitMeasureTable" style="width: 100%;" @selection-change="handleSelectionChange">

                        <el-table-column type="selection" width="55">
                        </el-table-column>

                        <el-table-column prop="measureId" label="单位ID" v-if="false">
                        </el-table-column>
                        <el-table-column prop="measureCode" label="单位编码">
                        </el-table-column>
                        <el-table-column prop="measureName" label="单位名称">
                        </el-table-column>
                        <el-table-column prop="primaryFlag" label="是否是主单位">
                            <template slot-scope="scope">
                                <el-tag v-if="scope.row.primaryFlag === 'Y'">是</el-tag>
                                <el-tag v-else type="danger">否</el-tag>
                            </template>
                        </el-table-column>
                        <el-table-column prop="changeRate" label="与主单位换算比例">
                        </el-table-column>
                        <el-table-column prop="enableFlag" label="是否启用" width="120">
                            <template slot-scope="scope">
                                <el-tag v-if="scope.row.enableFlag === 'Y'">是</el-tag>
                                <el-tag v-else type="danger">否</el-tag>
                            </template>
                        </el-table-column>
                        <el-table-column prop="remark" label="备注">
                        </el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-col :span="12">
                                    <el-link type="primary" @click="changeDialog(scope.row.measureId)">修改</el-link>
                                </el-col>
                                <el-col :span="12">
                                    <el-link type="primary" @click="delDialog(scope.row.measureId)">删除</el-link>
                                </el-col>
                            </template>
                        </el-table-column>
                    </el-table>
                    <!-- 分页模块 -->
                    <el-pagination :page-size="searchUnit.pageSize" :total="searchUnit.total"
                        @current-change="searchUnitChange" layout="prev, pager, next">
                    </el-pagination>

                    <!-- dialog -->
                    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="50%" class="rounded-dialog"
                        :close-on-click-modal="false" @close="closeUnitDialog">
                        <el-form :model="AddUnitMeasure" :rules="rules" ref="unitMeasureForm"
                            style="padding-left:60px;">
                            <el-form-item label="单位编码" prop="measureCode">
                                <el-input v-model="AddUnitMeasure.measureCode" autocomplete="off"
                                    style="width: 570px;"></el-input>
                            </el-form-item>
                            <el-form-item label="单位名称" prop="measureName">
                                <el-input v-model="AddUnitMeasure.measureName" autocomplete="off"
                                    style="width: 570px;"></el-input>
                            </el-form-item>
                            <el-form-item label="是否是主单位" prop="primaryFlag">
                                <el-radio v-model="AddUnitMeasure.primaryFlag" label="Y">是</el-radio>
                                <el-radio v-model="AddUnitMeasure.primaryFlag" label="N">否</el-radio>
                            </el-form-item>
                            <el-form-item label="是否启用" prop="enableFlag">
                                <el-radio v-model="AddUnitMeasure.enableFlag" label="Y">是</el-radio>
                                <el-radio v-model="AddUnitMeasure.enableFlag" label="N">否</el-radio>
                            </el-form-item>
                            <el-form-item label="备注">
                                <el-input v-model="AddUnitMeasure.remark" autocomplete="off"
                                    style="width: 600px;"></el-input>
                            </el-form-item>
                        </el-form>
                        <div slot="footer" class="dialog-footer">
                            <el-button @click="closeUnitDialog">取 消</el-button>
                            <el-button type="primary" @click="submitForm('unitMeasureForm')">确 定</el-button>
                        </div>
                    </el-dialog>

                </div>
            </el-col>
        </el-row>

    </div>
</template>
<script>
import axios from 'axios';
import qs from 'qs';
export default {
    data() {
        return {
            rules: {
                measureCode: [
                    { required: true, message: '请输入单位编码', trigger: 'blur' },
                    { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
                ],
                measureName: [
                    { required: true, message: '请输入单位名称', trigger: 'blur' },
                    { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
                ],
                primaryFlag: [
                    { required: true, message: '请选择是否是主单位', trigger: 'change' }
                ],
                enableFlag: [
                    { required: true, message: '请选择是否启用', trigger: 'change' }
                ]
            },
            // 设置dialog默认不显示
            dialogFormVisible: false,
            //根据是否有id输入判断标题
            dialogTitle: '',
            // 分页查询数据
            searchUnit: {
                measureCode: '',
                measureName: '',
                pageSize: 5,
                pageNo: 1,
                total: 0,
            },
            // 查询显示表格
            UnitMeasureTable: [],
            //新增MdItem或者修改MdItem
            AddUnitMeasure: {
                measureId: '',
                measureCode: '',
                measureName: '',
                primaryFlag: '',
                enableFlag: '',
                remark: '',
            },
            selectedRows: [],
            judgmentCh: false,
            judgmentDe: false,
            //当前选中行的MeasureId的数组
            arrayMeasureId: [],
            // 获取当前选中行的MeasureId
            SelMeasureId: '',
            showSearch: true, // 控制搜索框的显示和隐藏
        }
    },
    mounted() {
        this.checkListUnit();
    },
    methods: {
        //刷新按钮
        refresh() {
            location.reload();
        },
        //是否显示搜索框
        toggleSearch() {
            // 切换搜索框的显示和隐藏
            this.showSearch = !this.showSearch;
        },
        //获取当前选中表格行的状态
        handleSelectionChange(selection) {
            this.selectedRows = selection;

            // 更新 arrayMeasureId
            this.arrayMeasureId = selection.map(row => row.measureId);
            
            if (selection.length === 1) {
                //获取单个id
                this.arrayMeasureId[0] = this.selectedRows[0].measureId
                this.SelMeasureId = this.selectedRows[0].measureId
                this.judgmentDe = true;
                this.judgmentCh = true;
            } else if (selection.length > 1) {
                //获取一组id
                let rowsLength = this.selectedRows.length
                for (let index = 0; index < rowsLength; index++) {
                    this.arrayMeasureId[index] = this.selectedRows[index].measureId;
                }
                this.judgmentCh = false;
                this.judgmentDe = true;
            } else {
                this.judgmentCh = false;
                this.judgmentDe = false;
            }
        },
        //根据传递进来的是数组还是单个的值来进行判断是调用什么方法
        delJudgDialog(arrayMeasureId) {
            //只有一个id调用删除一个id的方法
            if (arrayMeasureId.length === 1) {
                this.delDialog(this.SelMeasureId);
            } else {
                this.delUnitArray(arrayMeasureId)
            }
        },
        //根据传递进来的MeasureId数组来进行批量删除
        delUnitArray(arrayMeasureId) {
            this.$confirm("确定要删除" + this.arrayMeasureId + " 这" + this.arrayMeasureId.length + "条数据吗？", '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$axios.delete("http://localhost:9970/emsSystem/api/unit/delUnitArray", { data: arrayMeasureId }).then((res) => {
                    if (res.data.code === 200) {
                        //删除成功
                        this.$message.success("删除成功");
                        this.checkUnit();
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //根据id删除指定数据
        delDialog(measureId) {
            this.$confirm('确定要删除该条信息, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$axios.delete("http://localhost:9970/emsSystem/api/unit/delUnit/" + measureId).then((res) => {
                    if (res.data.code === 200) {
                        //删除成功
                        this.$message.success("删除成功");
                        this.checkUnit();
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //关闭dialog并且清空里面的数据
        closeUnitDialog() {
            // 重置表单字段
            this.$refs.unitMeasureForm.resetFields();
            this.dialogFormVisible = false
            // 可选：手动重置数据模型
            this.AddUnitMeasure = {
                measureId: '',
                measureCode: '',
                measureName: '',
                primaryFlag: 'N',
                enableFlag: 'N',
                remark: ''
            };
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    // 表单验证通过，执行提交操作
                    this.AddOrChUnit();
                } else {
                    console.log('表单验证失败');
                    return false;
                }
            });
        },
        //添加或者修改unit
        AddOrChUnit() {
            let qsUnit = qs.stringify(this.AddUnitMeasure);

            this.$axios.post("http://localhost:9970/emsSystem/api/unit/AddOrChUnit", qsUnit).then((res) => {
                if (res.data.code === 200) {
                    //修改成功
                    this.$message.success("修改或新增成功");
                    this.dialogFormVisible = false;
                    this.checkUnit();
                }
            })
        },
        //改变dialog显示状态
        changeDialog(id) {
            this.dialogFormVisible = true
            if (id == null) {
                this.dialogTitle = "添加单位"
            } else {
                this.dialogTitle = "修改单位"
                this.AddUnitMeasure.measureId = id
                this.checkMdItemById(id);
            }

        },
        //根据measureId查询数据
        checkMdItemById(measureId) {
            this.$axios.post("http://localhost:9970/emsSystem/api/unit/CheckUnitById/" + measureId).then((res) => {
                if (res.data.code === 200) {
                    this.AddUnitMeasure = res.data.data
                }
            })
        },
        // 改变页码
        searchUnitChange(value) {
            this.searchUnit.pageNo = value;
            this.checkListUnit();
        },
        // 进行条件搜索
        checkUnit() {
            this.checkListUnit();
        },
        // 清空搜索框
        cleanMdItem() {
            this.searchUnit.measureCode = ""
            this.searchUnit.measureName = ""
        },
        // 查询Unit数据方法
        checkListUnit() {

            let qsStr = qs.stringify(this.searchUnit);

            this.$axios.post("http://localhost:9970/emsSystem/api/unit/checkListUnit", qsStr).then((res) => {
                console.log(res);

                if (res.data.code === 200) {
                    this.UnitMeasureTable = res.data.data.records
                    this.searchUnit.pageNo = res.data.data.current
                    this.searchUnit.pageSize = res.data.data.size
                    this.searchUnit.total = res.data.data.total
                }
            })
        }
    }
}
</script>
<style>
.divShow {
    background-color: white;
    border-radius: 10px;
}

.rounded-dialog .el-dialog {
    border-radius: 10px;
    /* 设置圆角 */
}

/* 全局样式文件 */
.el-form-item__label {
    font-weight: bold;
}
</style>
